package at.pp.match3.activity;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.util.modifier.IModifier;

public class ExchangeCompletedDetector implements IEntityModifierListener {

    private StartActivity startActivity;
    private boolean checkMatch;
    private boolean isReversing;

    public ExchangeCompletedDetector(StartActivity startActivity,
            boolean checkMatch, boolean isReversing) {
        this.startActivity = startActivity;
        this.checkMatch = checkMatch;
        this.isReversing = isReversing;
    }

    @Override
    public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
        if (checkMatch) {
            if (isReversing) {
                startActivity.isMoving = false;
                startActivity.deselectSprite(startActivity.selectedSprite);
            } else {
                // check for match
                if (startActivity.random.nextBoolean()) {
                    startActivity.updateGrid();
                    startActivity.movingSprite = null;
                    startActivity.isMoving = false;
                    startActivity.deselectSprite(startActivity.selectedSprite);
                } else {
                    startActivity.reversetMovement();
                }
            }

        }
    }

}
